package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.*;
import com.easondongh.business.mapper.*;
import com.easondongh.business.vo.GoodsSalesQueryVo;
import com.easondongh.business.vo.GoodsSalesQueryVo;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.domain.GoodsSales;
import com.easondongh.business.mapper.GoodsSalesMapper;
import com.easondongh.business.service.GoodsSalesService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class GoodsSalesServiceImpl extends ServiceImpl<GoodsSalesMapper, GoodsSales> implements GoodsSalesService{
    
    @Autowired
    private GoodsSalesMapper goodsSalesMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CustomerMapper CustomerMapper;

    @Override
    public PageResult<GoodsSales> getByPage(BasePageQueryVo<GoodsSalesQueryVo> queryCondition) {
        IPage<GoodsSales> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<GoodsSales> queryWrapper = new QueryWrapper<>();
        GoodsSalesQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.eq(queryParams.getCustomerId() != null, GoodsSales.COL_CUSTOMER_ID, queryParams.getCustomerId());
            queryWrapper.eq(queryParams.getGoodsId() != null, GoodsSales.COL_GOODS_ID, queryParams.getGoodsId());
            queryWrapper.gt(queryParams.getStartTime() != null, GoodsSales.COL_SALES_TIME, queryParams.getStartTime());
            queryWrapper.lt(queryParams.getEndTime() != null, GoodsSales.COL_SALES_TIME, queryParams.getEndTime());
        }
        queryWrapper.orderByDesc(GoodsSales.COL_SALES_TIME);
        IPage<GoodsSales> iPage = this.goodsSalesMapper.selectPage(page, queryWrapper);
        List<GoodsSales> goodsSalesList = iPage.getRecords();
        if(!CollectionUtils.isEmpty(goodsSalesList)){
            List<Customer> providerList = this.CustomerMapper.getAllEnableCustomer();
            List<Goods> goodsList = this.goodsMapper.getAllEnableGoods();
            Map<Integer, String> customerMap = new HashMap<>();
            Map<Integer, String> goodsMap = new HashMap<>();
            if(!CollectionUtils.isEmpty(providerList)) {
                customerMap = providerList.stream().collect(Collectors.toMap(Customer::getId, Customer::getCustomerName));
            }
            if(!CollectionUtils.isEmpty(goodsList)) {
                goodsMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getGoodsName));
            }
            for (GoodsSales goodsSales : goodsSalesList) {
                goodsSales.setGoodsName(goodsMap.get(goodsSales.getGoodsId()));
                goodsSales.setCustomerName(customerMap.get(goodsSales.getCustomerId()));
            }
        }
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), goodsSalesList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addGoodsSales(GoodsSales goodsSales) {
        this.goodsSalesMapper.insert(goodsSales);
        this.goodsMapper.updateStock(goodsSales.getGoodsId(), 0 - goodsSales.getNumber());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> ids) {
        List<GoodsSales> importList = this.goodsSalesMapper.selectBatchIds(ids);
        for (GoodsSales goodsSales : importList) {
            this.goodsMapper.updateStock(goodsSales.getGoodsId(), goodsSales.getNumber());
        }
        this.goodsMapper.deleteBatchIds(ids);
    }
}
